package com.swgk.core.base.model.entity;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.provider.ContactsContract;
import android.text.TextUtils;

import com.swgk.core.mmkvutil.MkvUtils;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.util.StringUtil;
import com.swgk.core.util.SystemUtil;

import java.io.Serializable;
import java.util.Date;
import java.util.UUID;

/**
 * @author 张驰
 * @date 2021/7/16 9:35
 * @Description 日志上报传参
 * @name com.swgk.core.base.model.entity
 */
public class BehaviourFlowLog {
    private String who;//加入登录，此处值为userNo，否则为随机字符码
    private String uuId;//uuId，随机用户标识
    private long when; //调用时间戳
    private String where;//表示时间发生的页面，app可以根据实体情况决定是否需要填写
    private String what;//标识操作事件，指定标识命令，
    private String operation; //表示操作事件，指定标识命令，可以于what值相同
    private String plateform = "android";//平台标识：android，ios，waapp
    private String referer = "直接访问";
    private String source = "jyb-jyb-app"; //来源标识，选填：jyb-xxx-app/qgb-xxx-app,xxx代表各子app，例如：jyb-cailiao-app
    private String module = "home";//标识事件发生模块，可以取值进行自定义
    private int verson = 1;
    private DataExtra extra = new DataExtra();

    /**
     * @param where     标识时间发生的页面，app可以根据实体情况决定是否需要填写
     * @param what      标识操作事件，指定标识命令，
     * @param operation 标识操作事件，指定标识命令，可以于what值相同
     * @param module    标识事件发生模块，可以取值进行自定义
     */
    public BehaviourFlowLog(String where, String what, String operation, String module) {
        String userNo = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.USER_NO, "");
        String uuId = MkvUtils.get().getString(MkvUtils.CommomData, MkvUtils.APPUUID, "");
        if (TextUtils.isEmpty(uuId)) {
            uuId = UUID.randomUUID().toString();
            MkvUtils.get().putString(MkvUtils.CommomData, MkvUtils.APPUUID, UUID.randomUUID().toString());
        }
        this.who = userNo;
        this.uuId = uuId;
        this.when = new Date().getTime(); //调用时间戳
        this.where = where;
        this.what = what;
        this.operation = operation;
        this.plateform = "android";
        this.referer = "直接访问";
        this.source = "jyb-app"; //来源标识，选填：jyb-xxx-app/qgb-xxx-app,xxx代表各子app，例如：jyb-cailiao-app
        this.module = module;
    }

    public String getUuId() {
        return uuId;
    }

    public void setUuId(String uuId) {
        this.uuId = uuId;
    }

    public String getWho() {
        return who;
    }

    public void setWho(String who) {
        this.who = who;
    }

    public long getWhen() {
        return when;
    }

    public void setWhen(long when) {
        this.when = when;
    }

    public String getWhere() {
        return where;
    }

    public void setWhere(String where) {
        this.where = where;
    }

    public String getWhat() {
        return what;
    }

    public void setWhat(String what) {
        this.what = what;
    }

    public String getOperation() {
        return operation;
    }

    public void setOperation(String operation) {
        this.operation = operation;
    }

    public String getPlateform() {
        return plateform;
    }

    public void setPlateform(String plateform) {
        this.plateform = plateform;
    }

    public String getReferer() {
        return referer;
    }

    public void setReferer(String referer) {
        this.referer = referer;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public String getModule() {
        return module;
    }

    public void setModule(String module) {
        this.module = module;
    }

    public int getVerson() {
        return verson;
    }

    public void setVerson(int verson) {
        this.verson = verson;
    }

    public DataExtra getExtra() {
        return extra;
    }

    public void setExtra(DataExtra extra) {
        this.extra = extra;
    }

    public static class DataExtra {
        private DataOsInfo osInfo = new DataOsInfo(); //设备信息
        private DataTarget target = new DataTarget(); //额外信息，可根据情况进行增减

        public DataOsInfo getOsInfo() {
            return osInfo;
        }

        public void setOsInfo(DataOsInfo osInfo) {
            this.osInfo = osInfo;
        }

        public DataTarget getTarget() {
            return target;
        }

        public void setTarget(DataTarget target) {
            this.target = target;
        }

        public static class DataOsInfo {
            private String osVersion = "Android " + SystemUtil.getSystemVersion(); // 系统版本
            private String platform = "android"; //平台
            private String deviceBrand = SystemUtil.getDeviceBrand(); //手机厂商
            private String mobileModel = SystemUtil.getSystemModel(); //手机型号
            private String appVersion; //app版本


            public String getOsVersion() {
                return osVersion;
            }

            public void setOsVersion(String osVersion) {
                this.osVersion = osVersion;
            }

            public String getPlatform() {
                return platform;
            }

            public void setPlatform(String platform) {
                this.platform = platform;
            }

            public String getDeviceBrand() {
                return deviceBrand;
            }

            public void setDeviceBrand(String deviceBrand) {
                this.deviceBrand = deviceBrand;
            }

            public String getMobileModel() {
                return mobileModel;
            }

            public void setMobileModel(String mobileModel) {
                this.mobileModel = mobileModel;
            }

            public String getAppVersion() {
                return appVersion;
            }

            public void setAppVersion(String appVersion) {
                this.appVersion = appVersion;
            }
        }

        public static class DataTarget {
            private String cityCode = "";
            private String provinceCode = "";
            private String provinceName = "";
            private String cityName = "";
            private long startTime;
            private long endTime;
            private long duration;

            public DataTarget() {
                cityCode = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.adCode, "");
                provinceName = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.Province, "");
                cityName = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.City, "");
                if (TextUtils.isEmpty(cityCode)) {
                    cityCode = "650000";
                }
                provinceCode = cityCode.substring(0, 2) + "0000";
                if (TextUtils.isEmpty(provinceName)) {
                    provinceName = "新疆维吾尔自治区";
                }
            }

            public long getStartTime() {
                return startTime;
            }

            public void setStartTime(long startTime) {
                this.startTime = startTime;
            }

            public long getEndTime() {
                return endTime;
            }

            public void setEndTime(long endTime) {
                this.endTime = endTime;
            }

            public long getDuration() {
                return duration;
            }

            public void setDuration(long duration) {
                this.duration = duration;
            }

            public String getProvinceCode() {
                return provinceCode;
            }

            public void setProvinceCode(String provinceCode) {
                this.provinceCode = provinceCode;
            }

            public String getProvinceName() {
               if ("新疆".equals(provinceName)){
                    provinceName = "新疆维吾尔自治区";
                }
                return provinceName;
            }

            public void setProvinceName(String provinceName) {
                this.provinceName = provinceName;
            }

            public String getCityCode() {
                return cityCode;
            }

            public void setCityCode(String cityCode) {
                this.cityCode = cityCode;
            }

            public String getCityName() {
                return cityName;
            }

            public void setCityName(String cityName) {
                this.cityName = cityName;
            }
        }
    }

    @Override
    public String toString() {
        return "{" +
                "who='" + who + '\'' +
                ", when=" + when +
                ", where='" + where + '\'' +
                ", what='" + what + '\'' +
                ", operation='" + operation + '\'' +
                ", plateform='" + plateform + '\'' +
                ", referer='" + referer + '\'' +
                ", source='" + source + '\'' +
                ", module='" + module + '\'' +
                ", verson=" + verson +
                ", extra=" + extra +
                '}';
    }
}
